package com.neymar.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neymar.entity.Applicant;
import com.neymar.entity.Log;
import com.neymar.entity.Recruiter;
import com.neymar.entity.User;
import com.neymar.mapper.ApplicantMapper;
import com.neymar.mapper.LogMapper;
import com.neymar.mapper.RecruiterMapper;
import com.neymar.service.ApplicantService;
import com.neymar.service.LogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neymar.service.RecruiterService;
import com.neymar.service.UserService;
import com.neymar.util.LogUtils;
import com.neymar.util.RandomID;
import com.neymar.vo.LogVO;
import com.neymar.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 日志表 服务实现类
 * </p>
 *
 * @author neymar
 * @since 2021-06-02
 */
@Service
public class LogServiceImpl implements LogService {

    @Autowired
    private LogMapper logMapper;

    @Autowired
    private ApplicantMapper applicantMapper;

    @Autowired
    private RecruiterService recruiterService;

    @Autowired
    private UserService userService;

    @Override
    public int save(String userId, int operationType, int objectionType) {
        Log logInfo = LogUtils.getLogInfo(userId, operationType, objectionType);
        logInfo.setLogId(RandomID.getLogID());
        String name = userService.findById(userId);
        logInfo.setUserId(userId);
        logInfo.setUserName(name);
        return logMapper.insert(logInfo);
    }

    @Override
    public LogVO findLogByUserId(String userId, Integer current, Integer size) {
        // 分页查询
        IPage<Log> iPage = new Page<>();
        iPage.setCurrent(current);
        iPage.setSize(size);
        QueryWrapper<Log> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        iPage = logMapper.selectPage(iPage, wrapper);
        LogVO logVO = new LogVO();
        logVO.setCurrent(current);
        logVO.setSize(size);
        logVO.setPages(iPage.getPages());
        logVO.setTotal(iPage.getTotal());
        logVO.setLogs(iPage.getRecords());
        return logVO;
    }

    @Override
    public LogVO findLogByTime(Date startTime, Date endTime, Integer current, Integer size) {
        // 分页查询
        IPage<Log> iPage = new Page<>();
        iPage.setCurrent(current);
        iPage.setSize(size);
        QueryWrapper<Log> wrapper = new QueryWrapper<>();
        wrapper.between("create_time", startTime, endTime);
        iPage = logMapper.selectPage(iPage, wrapper);
        LogVO logVO = new LogVO();
        logVO.setCurrent(current);
        logVO.setSize(size);
        logVO.setPages(iPage.getPages());
        logVO.setTotal(iPage.getTotal());
        logVO.setLogs(iPage.getRecords());
        return logVO;
    }

    @Override
    public LogVO findAllLog(Integer current, Integer size) {
        // 分页查询
        IPage<Log> iPage = new Page<>();
        iPage.setCurrent(current);
        iPage.setSize(size);
//        QueryWrapper<Log> wrapper = new QueryWrapper<>();
//        wrapper.select("user_id", "user_name", "user_role");
        iPage = logMapper.selectPage(iPage, null);

        LogVO logVO = new LogVO();
        logVO.setCurrent(current);
        logVO.setSize(size);
        logVO.setPages(iPage.getPages());
        logVO.setTotal(iPage.getTotal());
        logVO.setLogs(iPage.getRecords());
        return logVO;
    }


    @Override
    public List showTypeInfo() {
        QueryWrapper<Log> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("log_type", "count(*) as number").groupBy("log_type");
        List<Map<String, Object>> list = logMapper.selectMaps(queryWrapper);
        return list;
    }
}
